package com.itheima.controller.user;

import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        log.info("{}",addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();

    }

    /**
     * 查看所有地址
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<AddressBook> list=addressBookService.list();
        return Result.success(list);
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public Result getAddressDefault(){
        AddressBook addressBook=addressBookService.getDefaultAddress();
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据地址id删除地址
     * @return
     */
    @DeleteMapping
    public Result delete(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }


}
